<?php
	if(isset($_POST['btaddsp']))
	$tensp = $_POST['tensp'];
	$tenspjp = $_POST['tenspjp'];
	$loaisp = $_POST['loaisp'];
	$ld = $_POST['loader'];
	$gia = $_POST['giasp'];
	$ggia = $_POST['giamgiasp'];
	$tg = $_POST['tgxep'];
	$kt = $_POST['ktmau'];
	$ton = $_POST['ton'];
	$part="upload/";
	$thum="upload/thumbs/";
if(isset($_POST['btaddsp']))
{
	$ftype1 = $_FILES['file1']['type'];
	$fname1 = $_FILES['file1']['name'];
	$fsize1 = $_FILES['file1']['size'];
	$ftmp1 = $_FILES['file1']['tmp_name'];
	if($ftype1 == "image/pjpeg" || $ftype1 == "image/jpeg")
	{
		$new_img1 = imagecreatefromjpeg($ftmp1);
	}elseif($ftype1 == "image/x-png" || $ftype1 == "image/png")
	{
		$new_img1 = imagecreatefrompng($ftmp1);
	}elseif($ftype1 == "image/gif")
	{
		$new_img1 = imagecreatefromgif($ftmp1);
	}
	list($width1,$height1)=getimagesize($ftmp1);
	$newwidth = 96;
	$newheight1 = floor($height1*($newwidth/$width1));
	$resized_img1 = imagecreatetruecolor($newwidth,$newheight1);
	if ((($ftype1 == "image/gif")||($ftype1 == "image/jpeg")||($ftype1 == "image/pjpeg"))&&($fsize1 < 5242880))
	{
		if ($_FILES["file1"]["error"] > 0)
		{
			echo "Return Code: ".$_FILES["file1"]["error"]."<br />";
		}else
		{
			if (file_exists($part.$fname1))
			{
				echo $fname1." already exists.";
			}else
			{
				$rand1 = rand(0,999999999);
				copy ($ftmp1,$part . $fname1);
				rename ($part.$fname1,$part."origamishop.".$rand1.'.'.$fname1);
				$rename1='origamishop.'.$rand1.'.'.$fname1;
				$i1=$host["l_link"].$part.$rename1;
				$t1=$host["l_link"].$thum.$rename1;
			}
		}
	}else
	{
		echo "File có size lớn hơn 5Mb";
	}
	imagecopyresized($resized_img1, $new_img1, 0, 0, 0, 0, $newwidth, $newheight1, $width1, $height1);
	imagejpeg ($resized_img1,$thum.$rename1);
	$ftype2 = $_FILES['file2']['type'];
	$fname2 = $_FILES['file2']['name'];
	$fsize2 = $_FILES['file2']['size'];
	$ftmp2 = $_FILES['file2']['tmp_name'];
	if($ftype2 == "image/pjpeg" || $ftype2 == "image/jpeg")
	{
		$new_img2 = imagecreatefromjpeg($ftmp2);
	}elseif($ftype2 == "image/x-png" || $ftype2 == "image/png")
	{
		$new_img2 = imagecreatefrompng($ftmp2);
	}elseif($ftype2 == "image/gif")
	{
		$new_img2 = imagecreatefromgif($ftmp2);
	}
	list($width2,$height2)=getimagesize($ftmp2);
	$newheight2 = floor($height2*($newwidth/$width2));
	$resized_img2 = imagecreatetruecolor($newwidth,$newheight2);
	if ((($ftype2 == "image/gif")||($ftype2 == "image/jpeg")||($ftype2 == "image/pjpeg"))&&($fsize2 < 5242880))
	{
		if ($_FILES["file2"]["error"] > 0)
		{
			echo "Return Code: ".$_FILES["file2"]["error"]."<br />";
		}else{
			if (file_exists($part.$fname2))
			{
				echo $fname2." already exists.";
			}else{
				$rand2 = rand(0,999999999);
				copy ($ftmp2,$part . $fname2);
				rename ($part.$fname2,$part."origamishop.".$rand2.'.'.$fname2);
				$rename2='origamishop.'.$rand2.'.'.$fname2;
				$i2=$host["l_link"].$part.$rename2;	
				$t2=$host["l_link"].$thum.$rename2;
			}
		}
	}else
	{
		echo "File có size lớn hơn 5Mb";
	}
	$ftype3 = $_FILES['file3']['type'];
	$fname3 = $_FILES['file3']['name'];
	$fsize3 = $_FILES['file3']['size'];
	$ftmp3 = $_FILES['file3']['tmp_name'];
	if($ftype3 == "image/pjpeg" || $ftype3 == "image/jpeg")
	{
		$new_img3 = imagecreatefromjpeg($ftmp3);
	}elseif($ftype3 == "image/x-png" || $ftype3 == "image/png")
	{
		$new_img3 = imagecreatefrompng($ftmp3);
	}elseif($ftype3 == "image/gif")
	{
		$new_img3 = imagecreatefromgif($ftmp3);
	}
	list($width3,$height3)=getimagesize($ftmp3);
	$newheight3 = floor($height3*($newwidth/$width3));
	$resized_img3= imagecreatetruecolor($newwidth,$newheight3);
	if ((($ftype3 == "image/gif")||($ftype3 == "image/jpeg")||($ftype3 == "image/pjpeg"))&&($fsize3 < 5242880))
	{
		if ($_FILES["file3"]["error"] > 0)
		{
			echo "Return Code: ".$_FILES["file3"]["error"]."<br />";
		}else
		{
			if (file_exists($part.$fname3))
			{
				echo $fname3." already exists.";
			}else
			{
				$rand3 = rand(0,999999999);
				copy ($ftmp3,$part . $fname3);
				rename ($part.$fname3,$part."origamishop.".$rand3.'.'.$fname3);
				$rename3='origamishop.'.$rand3.'.'.$fname3;
				$i3=$host["l_link"].$part.$rename3;
				$t3=$host["l_link"].$thum.$rename3;
			}
		}
	}else
	{
		echo "File có size lớn hơn 5Mb";
	}
	$ftype4 = $_FILES['file4']['type'];
	$fname4 = $_FILES['file4']['name'];
	$fsize4 = $_FILES['file4']['size'];
	$ftmp4 = $_FILES['file4']['tmp_name'];
	if($ftype4 == "image/pjpeg" || $ftype4 == "image/jpeg")
	{
		$new_img4 = imagecreatefromjpeg($ftmp4);
	}elseif($ftype4 == "image/x-png" || $ftype4 == "image/png")
	{
		$new_img4 = imagecreatefrompng($ftmp4);
	}elseif($ftype4 == "image/gif")
	{
		$new_img4 = imagecreatefromgif($ftmp4);
	}
	list($width4,$height4)=getimagesize($ftmp4);
	$newheight4 = floor($height4*($newwidth/$width4));
	$resized_img4= imagecreatetruecolor($newwidth,$newheight4);
	if ((($ftype4 == "image/gif")||($ftype4 == "image/jpeg")||($ftype4 == "image/pjpeg"))&&($fsize4 < 5242880))
	{
		if ($_FILES["file4"]["error"] > 0)
		{
			echo "Return Code: ".$_FILES["file4"]["error"]."<br />";
		}else
		{
			if (file_exists($part.$fname4))
			{
				echo $fname4." already exists.";
			}else
			{
				$rand4 = rand(0,999999999);
				copy ($ftmp4,$part . $fname4);
				rename ($part.$fname4,$part."origamishop.".$rand4.'.'.$fname4);
				$rename4='origamishop.'.$rand4.'.'.$fname4;
				$i4=$host["l_link"].$part.$rename4;
				$t4=$host["l_link"].$thum.$rename4;
			}
		}
	}else
	{
		echo "File có size lớn hơn 5Mb";
	}
	imagecopyresized($resized_img2, $new_img2, 0, 0, 0, 0, $newwidth, $newheight2, $width2, $height2);
	imagejpeg ($resized_img2,$thum.$rename2);
	imagecopyresized($resized_img3, $new_img3, 0, 0, 0, 0, $newwidth, $newheight3, $width3, $height3);
	imagejpeg ($resized_img3,$thum.$rename3);
	imagecopyresized($resized_img4, $new_img4, 0, 0, 0, 0, $newwidth, $newheight4, $width4, $height4);
	imagejpeg ($resized_img4,$thum.$rename4);
	if($tensp == "" || $tenspjp == "" || $gia == "" || $ggia == "" || $kt == "" || $ton == "")
	{
		echo "tên sản phẩm, giá sản phẩm, kích thước, link hình không được bỏ trống <a href='javascript:history.back(-1);'>Back</a>";
	}
	else 
	{
	$query = "INSERT INTO sp VALUES ('',n'$tensp','$gia',n'$kt',n'$ld',n'$tg','false','0','$ton',n'$tenspjp',n'$loaisp','$i4','$i1','$i2','$i3','-$ggia','0','$t1','$t2','$t3','$t4')";
	$result = DataProvider::ExecuteQuery($query);
	echo '
	<meta http-equiv="refresh" content="2; url='.$host["l_link"].'" />
	Thêm sản phẩm mới thành công!
	';
	}
	}
?>